import { FormGroup, FormArray } from '@angular/forms';

const fnCheckForm = function checkForm(form: FormGroup): boolean {
  Object.keys(form.controls).forEach(key => {
    form.controls[key].markAsDirty();
    form.controls[key].updateValueAndValidity();
  });
  return form.invalid;
};

const fnResteForm = function resteForm(form: FormGroup): void {
  form.reset();
  for (const key in form.controls) {
    if (form.controls.hasOwnProperty(key)) {
      form.controls[key].markAsPristine();
      form.controls[key].updateValueAndValidity();
    }
  }
};

// 清空formArray
const fnClearFormArray = function clearFormArray(formArray: FormArray): void {
  while (formArray.length !== 0) {
    formArray.removeAt(0);
  }
};

export { fnClearFormArray, fnCheckForm, fnResteForm };
